package com.xszx.server.service;

import com.xszx.server.dto.Page;
import com.xszx.server.entity.Teacher;
import com.xszx.server.mapper.TeacherMapper;
import com.xszx.server.util.UuidUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.util.List;

@Service
public class TeacherService {
    @Resource
    private TeacherMapper teacherMapper;

    /**
     * 获取列表
     * @param
     * @return
     */
    public List<Teacher> getAll(){
        return teacherMapper.findAllTeachers();
    }
    /**
     * 获取分页列表
     * @param page
     * @return
     */
    public Page getAllTeachers(Page page){
        int pageNum = page.getPage();
        page.setPage((page.getPage()-1)*page.getSize());
        page.setList(teacherMapper.findAll(page));
        page.setTotal(teacherMapper.findCount());
        page.setPage(pageNum);
        return page;
    }

    /**
     * 新增或者删除信息
     * @param teacher
     * @return
     */
    public String saveOrUpdate(Teacher teacher){
        if(StringUtils.isEmpty(teacher.getId())){
            teacher.setId(UuidUtil.getShortUuid());
            return (teacherMapper.insert(teacher)>0)?"添加成功":"添加失败";
        }else{
            return (teacherMapper.updateByPrimaryKey(teacher)>0)?"更新成功":"更新失败";
        }
    }

    /**
     * 物理删除讲师
     * @param id
     * @return
     */
    public int del(String id){
        return teacherMapper.deleteByPrimaryKey(id);
    }
     /**
     * 获取单个讲师
     * @return
     */
    public Teacher getTeacher(String id){
        return teacherMapper.selectByPrimaryKey(id);
    }
}
